package com.hjm.strategy;

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AgentStrategyContext {
    @Resource
    private List<AgentStrategy> strategies;
    private final Map<String, AgentStrategy> strategyMap = new HashMap<>();
    @PostConstruct
    public void init() {
        for (AgentStrategy strategy : strategies) {
            strategyMap.put(strategy.getClass().getAnnotation(Channel.class).value(), strategy);
        }
    }
    public AgentStrategy getStrategy(String channelCode) {
        AgentStrategy strategy = strategyMap.get(channelCode);
        if (strategy == null) {
            throw new RuntimeException("未找到渠道策略: " + channelCode);
        }
        return strategy;
    }
}
